﻿// client.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"
#include "global.h"

using namespace std;
using namespace ted;

void TestNonBlocking();
void TestBlocking();
void TestEventSelect();


int _tmain(int argc, _TCHAR* argv[])
{
	//TestNonBlocking();
	//TestBlocking();
	TestEventSelect();
	return 0;
}


void TestEventSelect() {
	SocketHandler* handler = new SocketHandler();
	EventSelectClient client(handler, _T("127.0.0.1"), 8080);
	IoSession* session = client.Connect();
	if(session==NULL) {
		cout<<"Fail to connect"<<endl;
		return;
	}


	
		IoBuffer& buf = *new IoBuffer(128);	
		buf.AddByte(1);
		buf.AddFloat(2.0);
		buf.AddInt16(3);
		buf.AddInt32(4);
		buf.AddInt64(5);
		buf.Flip();

		session->Send(buf);	
		delete &buf;
	while(true) {
		Sleep(1000);
	}
}

void TestBlocking() {
	BlockingClient client(_T("127.0.0.1"), 8080);

	IoBuffer& buf = *new IoBuffer(128);	
	buf.AddByte(1);
	buf.AddFloat(2.0);
	buf.AddInt16(3);
	buf.AddInt32(4);
	buf.AddInt64(5);
	buf.Flip();

	IoBuffer& recvBuf = *new IoBuffer(128);
	bool result = client.Send(buf,recvBuf);
	result = client.Send(buf,recvBuf);

	delete &buf;
	delete &recvBuf;

}

void TestNonBlocking() {
	SocketHandler* handler = new SocketHandler();
	NonBlockingClient client(handler, _T("127.0.0.1"), 8080);
	IoSession* session = client.Connect();
	if(session==NULL) {
		cout<<"Fail to connect"<<endl;
		return;
	}

	IoBuffer& buf = *new IoBuffer(128);	
	buf.AddTChars(const_cast<TCHAR*>(_T("2")));
	buf.Flip();
	buf.ToHexDump();
	session->Send(buf);	

	IoBuffer& buf2 = *new IoBuffer(128);	
	buf2.AddByte(1);
	buf2.AddFloat(2.0);
	buf2.AddInt16(3);
	buf2.AddInt32(4);
	buf2.AddInt64(5);
	buf2.Flip();
	session->Send(buf2);
	
	delete &buf;
	delete &buf2;

	while(true) {
		Sleep(1000);
	}
}

class ClientHandler: public SocketHandler {
};

